//package ipad.asia.controller;
//
//import ipad.asia.dao.SecKillOrderDao;
//import ipad.asia.dao.SecKillProductDao;
//import ipad.asia.entity.SeckillOrder;
//import ipad.asia.entity.SeckillProduct;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//@Component
//public class SecKillThread implements Runnable{
//    @Autowired
//    private SecKillProductDao secKillProductDao;
//    @Autowired
//    private SecKillOrderDao secKillOrderDao;
//    public void count ( int productId, int consumerId, SecKillProductDao secKillProductDao, SecKillOrderDao
//        secKillOrderDao) {
//        synchronized (this) {
//            SeckillProduct seckillProduct = secKillProductDao.findByProductId(productId);
//            int count = seckillProduct.getNumber();
//            System.out.println("count::::" + count);
//            SeckillOrder seckillOrder = new SeckillOrder();
//            if (secKillProductDao.findByProductId(productId).getNumber() == 0) {
//                System.out.println("库存不足");
//            } else {
//                if (secKillOrderDao.findByConsumerIdAndProductId(consumerId, productId).size() > 0) {
//                    System.out.println("同一个人购买的件数: " + secKillOrderDao.findByConsumerIdAndProductId(consumerId, productId).size());
//                    System.out.println("每人只可以抢购一件");
//                } else {
//                    count--;
//                    seckillProduct.setNumber(count);
//                    secKillProductDao.saveAndFlush(seckillProduct);
//
//                    System.out.println("线程" + currentThread().getName() + "成功购买" + seckillProduct.getProductId());
//                    System.out.println("线程" + currentThread().getName() + "库存数量： " + secKillProductDao.findByProductId(productId).getNumber());
//
//                    seckillOrder.setConsumerId(consumerId).setProductId(productId);
//                    secKillOrderDao.saveAndFlush(seckillOrder);
//
//                    System.out.println("线程" + currentThread().getName() + "成功加入订单" + seckillProduct.getProductId());
//                    System.out.println("线程" + currentThread().getName() + "库存数量： " + secKillProductDao.findByProductId(productId).getNumber());
//                    System.out.println("------------------------------");
//                }
//            }
//        }
//    }
//
//    @Override
//    public void run() {
//        count(int productId, int consumerId, SecKillProductDao secKillProductDao, SecKillOrderDao
//        secKillOrderDao);
//    }
//}
